package com.icepeach.icepeachhomeback.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.icepeach.icepeachhomeback.common.RelationEnum;
import com.icepeach.icepeachhomeback.dto.AssignMenuRoleDto;
import com.icepeach.icepeachhomeback.entity.Menu;
import com.icepeach.icepeachhomeback.exception.Assert;
import com.icepeach.icepeachhomeback.req.MenuListQueryReq;
import com.icepeach.icepeachhomeback.service.MenuService;
import com.icepeach.icepeachhomeback.service.RelationService;
import com.icepeach.icepeachhomeback.util.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/menu")
public class MenuController {
    @Resource
    MenuService menuService;

    @Autowired
    private RelationService relationService;

    /**
     * 获取所有菜单
     *
     * @return
     */
    @PostMapping("/getAllMenu")
    @ResponseBody
    public ApiResult<List<Menu>> getAllMenu() {
        List<Menu> list = menuService.list(null);
        List<Menu> menus = menuService.convertToTree(list,null);
        return ApiResult.success("查询成功", menus);
    }

    /**
     * 根据id获取单个菜单
     *
     * @return
     */
    @PostMapping("/getMenuById")
    @ResponseBody
    public ApiResult getMenuById(String id) {
        if (StrUtil.isNotBlank(id)) {
            return ApiResult.success("查询成功", menuService.getById(id));
        } else {
            return ApiResult.failure("请求异常");
        }
    }




    /**
     * 增加或修改菜单
     *
     * @param menu
     * @return
     */

    @PostMapping("/saveMenu")
    @ResponseBody
    public ApiResult saveMenu(@RequestBody Menu menu) {
        boolean result = menuService.saveOrUpdate(menu);
        if (result) {
            return ApiResult.success("成功");
        } else {
            return ApiResult.failure("失败");
        }
    }

    @PostMapping("/deleteById")
    @ResponseBody
    public ApiResult deleteById(@RequestBody Menu menu) {
        boolean result = menuService.removeById(menu.getId());

        if (result) {
            return ApiResult.success("成功");
        } else {
            return ApiResult.failure("失败");
        }
    }

    @PostMapping("/del/batch")
    @ResponseBody
    public ApiResult deleteBatch(@RequestBody List<String> ids) {
        menuService.removeByIds(ids);
        return ApiResult.success("成功");
    }

    /**
     * 给角色分配菜单
     * @param assignMenuRoleDto
     * @return
     */
    @PostMapping("/assignMenus")
    @ResponseBody
    public ApiResult assignMenus(@RequestBody AssignMenuRoleDto assignMenuRoleDto){
        relationService.batchInsertRelationByTargetId(RelationEnum.ROLE_MENU.getBusinessType(),RelationEnum.ROLE_MENU.getTargetType(),
                assignMenuRoleDto.getRoleId(),assignMenuRoleDto.getRoleName(),assignMenuRoleDto.getMenuIds());
        return ApiResult.success("分配成功");
    }

    /**
     * 查询某角色下的菜单
     * @param assignMenuRoleDto
     * @return
     */
    @PostMapping("/queryMenuListByRole")
    @ResponseBody
    public ApiResult<List<Menu>> queryMenuListByRole(@RequestBody AssignMenuRoleDto assignMenuRoleDto){
        Assert.notNull(assignMenuRoleDto,"参数不能为空");
        Assert.notNull(assignMenuRoleDto.getRoleId(),"请选择角色");
        List<String> menuIds = menuService.queryMenuListByRole(assignMenuRoleDto.getRoleId());
        List<Menu> result = menuService.convertMenuToTree(menuIds);
        return ApiResult.success(result);
    }
}
